package com.wjs.juc.ThreadLocal;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.TimeUnit;

class House{
     public int saleNum=0;
     public synchronized void setSaleNum(){
         saleNum++;
     }
    ThreadLocal<Integer> saleCount=ThreadLocal.withInitial(()->0);

    public void setSaleCount(){
        saleCount.set(saleCount.get()+1);
    }
}
public class ThreadLocalDemo1 {
    public static void main(String[] args) throws InterruptedException {
        House house = new House();
        for (int i = 1; i <= 5; i++) {
            new Thread(()->{
                int j = new Random().nextInt(5)+1;
                for (int i1 = 0; i1 < j; i1++) {
                    house.setSaleNum();
                    house.setSaleCount();
                }
                System.out.println(Thread.currentThread().getName()+":"+house.saleCount.get());
            },String.valueOf(i)).start();
        }
        TimeUnit.SECONDS.sleep(2);
        System.out.println("共卖出了："+house.saleNum);
    }
}
